package com.project.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private File upload;		//上传的文件，在extjs 中对应 xtype:'fileuploadfield',  name:'upload'
	private String fileName;	//上传的文件名
	private String uploadContentType; //限制上传的后缀名
	private String savePath;
	private String caption;		//form中其他的上传的字段
	private Boolean success = true;	//json中返回的结果
	private String resultMsg = "ok" ;  //上传失败后，返回的错误结果
	

	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
		System.out.println("上传到tomcat中的临时文件名：" + upload.getPath());
	}
	public String getUploadFileName() {
		return fileName;
	}
	public void setUploadFileName(String fileName) throws UnsupportedEncodingException {
		System.out.println("上传的文件名：" +fileName);
		this.fileName = fileName;
	}
	public  String getUploadContentType() {
		return uploadContentType;
	}
	public  void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	
	public String execute() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest(); 
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		if(session.get("login")==null){
			 resultMsg = "为登录，不能上传。请先登录！ ";
			 System.out.println(resultMsg);
			 success = false;
			 return SUCCESS;
		 }
		String bathpath = request.getSession().getServletContext().getRealPath("/");
		if(session.get("right").equals("super")||session.get("right").equals("manager"))
			this.setSavePath(bathpath+"//admin");
		else if(session.get("right").equals("user")){
			this.setSavePath(bathpath+"//user");
			File userF = new File(this.getSavePath()+"//"+session.get("number"));
			if(!(userF.exists())){
				userF.createNewFile();
				this.setSavePath(this.getSavePath()+"//"+session.get("number"));
			}
		}
		//上面根据权限来判断上传文件的保存地址
		
		
	 try { //此处为最简单的文件拷贝方法
		java.io.FileInputStream is = new FileInputStream(upload);
		this.setUploadFileName(upload.getName());  
		java.io.FileOutputStream os = new FileOutputStream(this.savePath+"//"+fileName);
		byte buffer[] = new byte[8192];
		int count = 0;
		while ((count = is.read(buffer)) > 0)
			os.write(buffer, 0, count);
		os.close();
		is.close();
	 } catch (Exception e)
	 {
		 resultMsg = "上传文件失败，文件太大或没有选择上传的文件 ";
		 System.out.println(resultMsg);
		 success = false;
	 }
	 //此处会根据 success 和 resultMsg 返回 json
	 //{"resultMsg":"ok","success":true}
	return SUCCESS;
		
	}
	
	
	public void setCaption(String caption) {
		System.out.println("接收到的caption：" + caption );
		this.caption = caption;
	}
	public String getCaption() {
		return caption;
	}
	public Boolean getSuccess() {
		return success;
	}
	public String getResultMsg() {
		return resultMsg;
	}
}